<?php

namespace app\kaoqin\api;

use app\common\logic\Department;
use app\common\logic\KqBuqian;
use app\common\logic\OldRecord;
use app\common\logic\User;
use my\wechat\Message;

class Apply extends Common
{
    /**
     * 补签信息
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function supply()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $uid = getUid();

        $info = User::get(['userid' => $uid]);
        if (empty($info)) return $this->error('用户不存在');

        $ret = $info;

        $dept_id = $info['department'];
        $dept = Department::where(['id' => $dept_id])->value('name');
        $ret['dept'] = $dept;

        $count = KqBuqian::getCountInMonth($uid);
        $ret['count'] = $count;

        $ret['sp'] = c('wx_apply_userid');

        return $this->success($ret);
    }

    /**
     * 补签
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function add_supply()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $req = input('');

        $req['source'] = 'm';

        if (!isset($req['time']) || empty($req['time']))
            return $this->error('请填写补签时间');

        if (!isset($req['reason']) || empty($req['reason']))
            return $this->error('请填写补签原因');

        if (!isset($req['sp_uid']) || empty($req['sp_uid']))
            return $this->error('请选择审批人');

        $KqBuqian = new KqBuqian();
        $ret = $KqBuqian->addItem($req);

        if ($ret === false)
            return $this->error($KqBuqian->getError());

        $articles = [[
            'title' => $KqBuqian['name'] . '提交了补签申请',
            'description' => "时间：" . $KqBuqian['time'] . "\n原因：" . $KqBuqian['reason'],
            'url' => request()->domain() . '/kaoqin/buqian/list'
        ]];

        //发送通知
        $wx_apply_userid = is_array(c('root_userid')) ? c('root_userid') : [c('root_userid')];
        $wx_apply_userid[] = $req['sp_uid'];

        $touser = array_values(array_unique($wx_apply_userid));

        if (!empty($touser)) {
            Message::$touser = implode('|', $touser);
            Message::$agentid = c('wx_apply_agentid');
            Message::send('news', $articles);
        }

        return $this->success($ret);
    }

    /**
     * 补签审批
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function sp_supply()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $req = input('');
        $uid = getUid();
        $user = User::get(['userid' => $uid]);

        if (!isset($req['id']) || empty($req['id']))
            return $this->error('请选择要审批的补签信息');

        if (!isset($req['status']) || empty($req['status']))
            return $this->error('补签状态不能为空');

        if (!in_array($req['status'], ['-1', '1']))
            return $this->error('补签状态不合法');

        $id = $req['id'];
        $status = $req['status'];

        $bq = KqBuqian::get(['id' => $id]);

        if (empty($bq))
            return $this->error('补签信息不存在');

        if (!empty($bq['status']))
            return $this->error('该补签信息已审批');

        //检查审核人是不是指定的员工
//        $root_userid = is_array(c('root_userid')) ? c('root_userid') : [c('root_userid')];
//        $wx_apply_userid = is_array(c('wx_apply_userid')) ? c('wx_apply_userid') : [c('wx_apply_userid')];
//
//        if (!in_array($uid, array_values(array_unique(array_merge($root_userid, $wx_apply_userid)))))
//            return $this->error('你没有权限审批补签');

        $ret = KqBuqian::update(['status' => $status, 'sp_uid' => $uid, 'sp_name' => $user['name'], 'sp_time' => date('Y-m-d H:i:s')], ['id' => $id]);

        if ($ret === false)
            return $this->error('审批出现问题，请稍后重试');

        if ($status == -1) $reply_msg = $user['name'] . '拒绝了你的补签申请';
        else $reply_msg = $user['name'] . '同意了你的补签申请';

        if ($status == 1)
            OldRecord::create(['userId' => $bq['uid'], 'time' => $bq->getData('time'), 'from' => $bq['source']]);

        $articles = [[
            'title' => $reply_msg,
            'description' => "时间：" . $bq['time'] . "\n原因：" . $bq['reason'],
            'url' => request()->domain() . '/kaoqin/buqian/detail/id/' . $id
        ]];

        if (!empty($bq['uid'])) {
            Message::$touser = $bq['uid'];
            Message::$agentid = c('wx_apply_agentid');
            Message::send('news', $articles);
        }

        return $this->success('审批成功');
    }

    /**
     * 补签详情
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function get_supply()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $req = input('');

        if (!isset($req['id']) || empty($req['id']))
            return $this->error('请选择要查看的补签信息');

        $id = $req['id'];

        $ret = KqBuqian::get(['id' => $id]);
        if (empty($ret))
            return $this->error('补签信息不存在');

        $count = KqBuqian::getCountInMonth($ret['uid']);
        $ret['count'] = $count;

        return $this->success($ret);
    }

    /**
     * 补签列表
     * @return array|string
     * @author wb <pithyone@vip.qq.com>
     */
    public function lists_supply()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $req = input('');
        $uid = getUid();

        $where = [];

        if (isset($req['tag']) && $req['tag'] == 'record') {
            if (!isset($req['uid']) || empty($req['uid']))
                $where['uid'] = $uid;
            else
                $where['uid'] = $req['uid'];
        } else {
            $root_userid = is_array(c('root_userid')) ? c('root_userid') : [c('root_userid')];
            if (!in_array($uid, $root_userid))
                $where['sp_uid'] = $uid;

            if (isset($req['type']))
                $where['status'] = $req['type'];
        }

        $ret = KqBuqian::getList($where, 'create_time desc');

        return $this->success($ret);
    }

    public function supply_count()
    {
        if (!$this->checkAuth())
            return $this->_error;

        $uid = getUid();

        $where = [];

        $root_userid = is_array(c('root_userid')) ? c('root_userid') : [c('root_userid')];
        if (!in_array($uid, $root_userid))
            $where['sp_uid'] = $uid;

        $ret = KqBuqian::where($where)->field('status,count(id) count')->group('status')->select();

        if (!empty($ret)) {
            foreach ($ret as $k => $v) {
                if ($v['status'] == -1)
                    $ret[$k]['name'] = '已拒绝';
                elseif ($v['status'] == 0)
                    $ret[$k]['name'] = '待审批';
                elseif ($v['status'] == 1)
                    $ret[$k]['name'] = '已同意';
            }
        }

        return $this->success($ret);
    }
}